package com.select.controller;

import com.feign.common.R;
import com.feign.domain.FileInfo;
import com.feign.domain.bo.FileBo;
import com.feign.util.FileUtil;
import com.select.service.SelectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/select")
public class SelectController {

    @Autowired
    private SelectService selectService;

    @GetMapping("/test")
    public String test(){
        return "test";
    }

    /**
     * 查询根目录文件
     * @return 根目录文件集合
     */
    @GetMapping("/root")
    public R selectRootPath(){
        List<FileInfo> list = selectService.selectRootPath();
        return R.ok(list);
    }

    /**
     * 查询文件夹下的文件
     * @param fileBo
     * @return 文件夹下的文件集合
     */
    @PostMapping("/folderFile")
    public R<List<FileInfo>> selectFilePath(@RequestBody FileBo fileBo) throws Exception {
        File file = FileUtil.folderExist(fileBo.getFolderPath());
        List<FileInfo> list = selectService.selectFilePath(file);
        return R.ok(list);
    }

    /**
     * 查看文件夹大小
     * @param filePath 文件路径
     * @return 文件夹大小字节
     * @throws Exception 文件不存在异常,不是文件夹异常
     */
    @GetMapping("/fileSize")
    public R<Long> selectFileSize(String filePath) throws Exception {
        File file = FileUtil.folderExist(filePath);
        Long fileSize = FileUtil.getFileSize(file);
        return R.ok(fileSize);
    }

    /**
     * 搜索文件
     * @param name 文件名
     * @return 文件集合
     */
    @GetMapping("/name")
    public R selectName(String name,Long size) throws Exception {
        List<FileInfo> list = selectService.selectName(name,size);
        Long fileTotal = selectService.getFileTotal();
        selectService.removeFileTotal();
        return R.ok(list).msg(fileTotal.toString());
    }

}
